package com.personal.controller;

import com.personal.controller.result.BackCode;
import com.personal.controller.result.Result;
import com.personal.domain.Contact;
import com.personal.service.ContactService;
import com.personal.domain.obtain.KeyValue;
import com.personal.util.partDisplay.ContactPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @projectName: smartCommunity_springboot
 * @package: com.personal.controller
 * @className: ContactController
 * @author: bong~
 * @description: TODO
 * @date: 2023/8/10 14:44
 * @version: 1.0
 */

@RestController
@RequestMapping("/contact")
public class ContactController {


    @Autowired
    private ContactService contactService;
    @GetMapping("/All")
    public Result selectAll(){

        List<Contact> list=contactService.selectAll();
        return new Result(list!=null? BackCode.GET_OK:BackCode.GET_ERR,list);
    }


    @GetMapping()
    public Result selectPartAll(){

        List<ContactPart> list=contactService.selectPartAll();
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }


    @GetMapping("/search")
    public Result selectPartByKey(@RequestParam("enterpriseId") int enterpriseId){
        List<ContactPart> list=contactService.selectPartByKey(enterpriseId);
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }


    @GetMapping("/idAndName")
    public Result selectIdAndNameAll(){
        List<KeyValue> list=contactService.selectIdAndNameAll();
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }


    @PostMapping()
    public Result add(@RequestBody Contact contact){
        boolean flag=contactService.add(contact);
        return new Result(flag?BackCode.SAVE_OK:BackCode.SAVE_ERR,flag);
    }


    @GetMapping("/updateData")
    public Result getUpdateData(int id){
        Contact contact=contactService.getUpdateData(id);
        return new Result(contact!=null?BackCode.GET_OK:BackCode.GET_ERR,contact);
    }
    @PutMapping()
    public Result update(@RequestBody Contact contact){
        boolean flag=contactService.update(contact);
        return new Result(flag?BackCode.UPDATE_OK:BackCode.UPDATE_ERR,flag);
    }



    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        boolean flag=contactService.delete(ids);
        return new Result(flag?BackCode.DELETE_OK:BackCode.DELETE_ERR,flag);
    }

}
